package com.timguan.render.web.common;

import com.timguan.render.api.api.B2CMallServiceReturnCode;
import net.pocrd.entity.ServiceException;
import net.pocrd.entity.ServiceRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * app类型与专题的类型不匹配
 * Created by guankaiqiang on 2014/12/3.
 */
public class TopicTypeNotMatchListener extends ServiceExceptionListener {
    private static final Logger logger = LoggerFactory.getLogger(TopicTypeNotMatchListener.class);
    private static final String REDIRECT_INVOKE_HEAD = "fengqu-cms-redirecturl";

    public TopicTypeNotMatchListener() {
        super();
    }

    @Override
    public void initAcceptCodes() {
        setCode(B2CMallServiceReturnCode._C_TOPIC_TYPE_NOT_MATCH);
    }

    /**
     * app类型与专题的类型不匹配
     *
     * @param event
     * @param request
     * @param response
     * @return
     */
    @Override
    public ModelAndView serviceExceptionEventHandle(int event, ServiceException se, ServiceRuntimeException sre, HttpServletRequest request, HttpServletResponse response) {
        if (B2CMallServiceReturnCode._C_TOPIC_TYPE_NOT_MATCH == event) {
            try {
                if (!response.isCommitted()) {
                    String redirectURL = "/index.html";
                    Throwable cause = null;
                    if (se != null) {
                        cause = se.getCause();
                    }
                    if (sre != null) {
                        cause = sre.getCause();
                    }
                    if (cause != null) {
                        logger.info("cause:" + cause.getMessage(), cause);
                    }
                    if (cause != null && cause instanceof TopicTypeMissmatchException) {
                        TopicTypeMissmatchException topicTypeMissmatchException = ((TopicTypeMissmatchException) cause);
                        redirectURL = topicTypeMissmatchException.getRedirectUrl();
                        if (!response.isCommitted()) {
                            if (TopicTypeMissmatchException.RedirectType.REDIRECT_302 == topicTypeMissmatchException.getRedirectType()) {
                                response.sendRedirect(redirectURL);
                            } else {
                                response.setHeader(REDIRECT_INVOKE_HEAD, topicTypeMissmatchException.getRedirectUrl());
                                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                            }
                        }
                    } else {
                        if (!response.isCommitted()) {
                            response.sendRedirect(redirectURL);
                        }
                    }
                } else {
                    logger.warn("response has been commited!");
                }
            } catch (IOException e) {
                logger.error("redirect failed!", e);
            }
        }
        return null;
    }
}
